package com.example.infowall02.service.impl;

import com.example.infowall02.domain.CommentRequest;
import com.example.infowall02.mapper.CommentMappper;
import com.example.infowall02.service.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
public class CommentServiceImpl implements CommentService {

    @Autowired
    private CommentMappper commentMapper;

    //增
    @Override
    public boolean insertComment(CommentRequest commentRequest) {
        commentRequest.setCreateTime(new Date()); // 手动设置当前时间
        return commentMapper.insertComment(commentRequest);
    }
    //删
    @Override
    public int deleteComment(Integer commentId) {
        return commentMapper.deleteComment(commentId);
    }
    //查
    @Override
    public CommentRequest findCommentById(Integer commentId) {
        return commentMapper.findCommentById(commentId);
    }
    @Override
    public List<CommentRequest> findCommentsByArticleId(Integer articleId,int pageNow,int pageSize) {
        return commentMapper.findCommentsByArticleId(articleId,pageNow,pageSize);
    }
    @Override
    public boolean hasUserCommentedArticle(int userId, int articleId) {
        return commentMapper.countCommentsByUserAndArticle(userId, articleId) > 0;
    }
    @Override
    public boolean isCommentExists(Integer commentId) {
        CommentRequest commentRequest = commentMapper.findCommentById(commentId);
        return commentRequest != null;
    }

    @Override
    public int countCommentsByArticleId(Integer articleId) {
        return commentMapper.countCommentsByArticleId(articleId);
    }

    //改
    @Override
    public int updateComment(String content, Integer commentId, Date createTime) {
        return commentMapper.updateComment(content, commentId, createTime);
    }
}
